package wechat_v3

import (
	"context"
	"duoke/model"
	"duoke/utils/gconv"
	"duoke/utils/gstr"
	"encoding/json"
	"errors"
	"github.com/gin-gonic/gin"
	"github.com/go-pay/gopay"
	"github.com/go-pay/gopay/wechat"
)

var SeparateConfig = new(Config)

type Config struct {
	AppId          string `json:"app_id" form:"app_id"`
	PayMchId       string `json:"pay_mch_id" form:"pay_mch_id"`
	PayKey         string `json:"pay_key" form:"pay_key"`
	PayKeyV3       string `json:"pay_key_v3" form:"pay_key_v3"`
	PaySerialNo    string `json:"pay_serial_no" form:"pay_serial_no"`
	PayCertPath    string `json:"pay_cert_file_path" form:"pay_cert_file_path"`
	PayKeyFilePath string `json:"pay_key_file_path" form:"pay_key_file_path"`
}

func NewConfig(res map[string]interface{}) *Config {
	// 分账流程
	return &Config{
		AppId:          gconv.String(res["app_id"]),
		PayMchId:       gconv.String(res["pay_mch_id"]),
		PayKey:         gconv.String(res["pay_key"]),
		PayKeyV3:       gconv.String(res["pay_key_v3"]),
		PaySerialNo:    gconv.String(res["pay_serial_no"]),
		PayCertPath:    gconv.String(res["pay_cert_path"]),
		PayKeyFilePath: gconv.String(res["pay_cert_key_path"]),
	}
}
func (c *Config) SubAccount(ctx *gin.Context, req *model.ReqSubAccount) (*wechat.ProfitSharingAddReceiverResponse, error) {
	client := wechat.NewClient(c.AppId, c.PayMchId, c.PayKey, true)
	err := client.AddCertPemFilePath(c.PayCertPath, c.PayKeyFilePath)
	if err != nil {
		return nil, err
	}
	Bm := make(gopay.BodyMap)
	// 随机字符串
	str := gstr.Random(32)
	Bm.Set("sub_mch_id", req.SubMchId)
	Bm.Set("appid", c.AppId)
	Bm.Set("nonce_str", str)
	// 将下列装换成json
	m := make(map[string]interface{})
	m["type"] = req.ReceivierType
	m["account"] = req.ReceivierAccount
	if req.ReceivierType == "MERCHANT_ID" {
		m["name"] = req.ReceivierName
		m["relation_type"] = "SERVICE_PROVIDER"
	} else {
		m["relation_type"] = "PARTNER"
	}
	marshal, err := json.Marshal(m)
	if err != nil {
		return nil, err
	}
	// end
	Bm.Set("receiver", string(marshal)) // 与分账方的关系类型
	rsp, err := client.ProfitSharingAddReceiver(ctx, Bm)
	if err != nil {
		return rsp, err
	}
	if rsp.ReturnCode != "SUCCESS" {
		return nil, errors.New(rsp.ReturnMsg)
	}
	if rsp.ResultCode != "SUCCESS" {
		return nil, errors.New(rsp.ErrCode + rsp.ErrCodeDes)
	}
	return rsp, nil
}

// 开始分账
func (c *Config) Share(m map[string]string, data []map[string]interface{}) (*wechat.ProfitSharingResponse, error) {
	client := wechat.NewClient(c.AppId, c.PayMchId, c.PayKey, true)
	err := client.AddCertPemFilePath(c.PayCertPath, c.PayKeyFilePath)
	if err != nil {
		return nil, err
	}
	// 随机字符串
	str := gstr.Random(32)
	Bm := make(gopay.BodyMap)
	// 对数据进行编码
	receivers, err := json.Marshal(data)
	if err != nil {
		return nil, err
	}
	s := Bm.Set("mch_id", c.PayMchId).
		Set("sub_mch_id", gconv.String(m["sub_mch_id"])).
		Set("appid", c.AppId).
		Set("nonce_str", str).
		Set("transaction_id", gconv.String(m["transaction_id"])).
		Set("out_order_no", gconv.String(m["out_order_no"])).
		Set("receivers", gconv.String(receivers))
	rsp, err := client.ProfitSharing(context.Background(), s)
	if err != nil {
		return nil, err
	}
	return rsp, nil
}
